#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include "public.hpp"
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <cassert>
#include <cstdio>

int main()
{
    // 客户端无需创建管道文件，只需要打开对应的文件即可
    int wfd = open(fifoname.c_str(), O_WRONLY);
    if (wfd < 0)
    {
        std::cout << errno << "错误原因:" << strerror(errno) << std::endl;
        return 1;
    }

    // 正常通信
    char buffer[NUM];
    while (true)
    {
        std::cout << "请输入你的消息# ";
        char *msg = fgets(buffer, sizeof(buffer), stdin);
        assert(msg);
        (void)msg;

        buffer[strlen(buffer) - 1] = 0; // 去除输入内容后的回车
        if(strcasecmp(buffer, "quit") == 0) break;//输入quit结束对话
        ssize_t n = write(wfd, buffer, strlen(buffer));
        assert(n >= 0);//=表示忽略空字符串
        (void)n;
    }

    close(wfd);
    return 0;
}